package com.bjpowernode.crm.workbench.service.impl;

import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.workbench.domain.*;
import com.bjpowernode.crm.workbench.mapper.*;
import com.bjpowernode.crm.workbench.service.ClueService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 郭俊才
 * 2021/8/27
 */
@Service
public class ClueServiceImpl implements ClueService {
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private ClueRemarkMapper clueRemarkMapper;
    @Autowired
    private ClueActivityRelationMapper clueActivityRelationMapper;
    @Autowired
    private ContactsMapper contactsMapper;
    @Autowired
    private ContactsRemarkMapper contactsRemarkMapper;
    @Autowired
    private ContactsActivityRelationMapper contactsActivityRelationMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private CustomerRemarkMapper customerRemarkMapper;
    @Autowired
    private TranMapper tranMapper;
    @Autowired
    private TranHistoryMapper tranHistoryMapper;

    @Override
    public int saveSelective(Clue clue) {
        //设置id
        clue.setId(UUIDUtils.getUUID());
        //设置创建时间
        clue.setCreateTime(DateUtils.formatDateTime(new Date()));
        return clueMapper.insertSelective(clue);
    }

    @Override
    public int removeList(String[] ids) {
        return clueMapper.deleteList(ids);
    }

    @Override
    public int editByPrimaryKeySelective(Clue clue) {
        //设置修改时间
        clue.setEditTime(DateUtils.formatDateTime(new Date()));
        return clueMapper.updateByPrimaryKeySelective(clue);
    }

    @Override
    public Clue queryByPrimaryKey(String id) {
        return clueMapper.selectByPrimaryKey(id);
    }

    @Override
    public Clue queryByPrimaryKeyForDetails(String id) {
        return clueMapper.selectByPrimaryKeyForDetails(id);
    }


    @Override
    public PageInfo<Clue> queryByConditions(Clue clue, Integer pageNum, Integer pageSize) {
        //开启分页
        PageHelper.startPage(pageNum, pageSize);
        List<Clue> clueList = clueMapper.selectByConditions(clue);
        return new PageInfo<Clue>(clueList);
    }

    @Override
    public int saveConvertClue(Map<String, Object> map) {

        //1.根据线索id查找线索信息
        String clueId = (String) map.get("clueId");
        User user = (User) map.get("user");
        Clue clue = clueMapper.selectByPrimaryKey(clueId);
        //2.创建客户对象并从线索对象中取出客户信息
        Customer customer = new Customer();
        customer.setId(UUIDUtils.getUUID());
        customer.setOwner(user.getId());
        customer.setName(clue.getCompany());
        customer.setWebsite(clue.getWebsite());
        customer.setPhone(clue.getPhone());
        customer.setCreateBy(clue.getCreateBy());
        customer.setCreateTime(clue.getCreateTime());
        customer.setEditBy(clue.getEditBy());
        customer.setEditTime(clue.getEditTime());
        customer.setContactSummary(clue.getContactSummary());
        customer.setNextContactTime(clue.getNextContactTime());
        customer.setDescription(clue.getDescription());
        customerMapper.insertSelective(customer);
        //3.创建联系人对象并从线索对象中取出联系人信息
        Contacts contacts = new Contacts();
        contacts.setId(UUIDUtils.getUUID());
        contacts.setOwner(user.getId());
        contacts.setSource(clue.getSource());
        contacts.setCustomerId(customer.getId());
        contacts.setFullName(clue.getFullName());
        contacts.setAppellation(clue.getAppellation());
        contacts.setEmail(clue.getEmail());
        contacts.setMphone(clue.getMphone());
        contacts.setJob(clue.getJob());
        contacts.setCreateBy(clue.getCreateBy());
        contacts.setCreateTime(clue.getCreateTime());
        contacts.setEditBy(clue.getEditBy());
        contacts.setEditTime(clue.getEditTime());
        contacts.setContactSummary(clue.getContactSummary());
        contacts.setNextContactTime(clue.getNextContactTime());
        contacts.setDescription(clue.getDescription());
        contacts.setAddress(clue.getAddress());
        contactsMapper.insertSelective(contacts);
        //4.将线索备注信息移到联系人备注表和客户备注表中
        List<ClueRemark> clueRemarkList = clueRemarkMapper.selectByClueId(clueId);
        for (ClueRemark clueRemark : clueRemarkList) {
            //设置联系人备注
            ContactsRemark contactsRemark = new ContactsRemark();
            contactsRemark.setId(UUIDUtils.getUUID());
            contactsRemark.setNoteContent(clueRemark.getNoteContent());
            contactsRemark.setCreateBy(clueRemark.getCreateBy());
            contactsRemark.setCreateTime(clueRemark.getCreateTime());
            contactsRemark.setEditBy(clueRemark.getEditBy());
            contactsRemark.setEditTime(clueRemark.getEditTime());
            contactsRemark.setEditFlag(clueRemark.getEditFlag());
            contactsRemark.setContactsId(contacts.getId());
            contactsRemarkMapper.insertSelective(contactsRemark);
            //设置客户备注
            CustomerRemark customerRemark = new CustomerRemark();
            customerRemark.setId(UUIDUtils.getUUID());
            customerRemark.setNoteContent(clueRemark.getNoteContent());
            customerRemark.setCreateBy(clueRemark.getCreateBy());
            customerRemark.setCreateTime(clueRemark.getCreateTime());
            customerRemark.setEditBy(clueRemark.getEditBy());
            customerRemark.setEditTime(clueRemark.getEditTime());
            customerRemark.setEditFlag(clueRemark.getEditFlag());
            customerRemark.setCustomerId(customer.getId());
            customerRemarkMapper.insertSelective(customerRemark);
        }
        //5.将线索-市场活动关联信息转移到联系人-市场活动表中
        List<ClueActivityRelation> clueActivityRelationList = clueActivityRelationMapper.selectClueActivityRelationByClueId(clueId);
        for (ClueActivityRelation clueActivityRelation : clueActivityRelationList) {
            ContactsActivityRelation contactsActivityRelation = new ContactsActivityRelation();
            contactsActivityRelation.setId(UUIDUtils.getUUID());
            contactsActivityRelation.setContactsId(contacts.getId());
            contactsActivityRelation.setActivityId(clueActivityRelation.getActivityId());
            contactsActivityRelationMapper.insertSelective(contactsActivityRelation);
        }
        //6.如果有创建交易需求，则创建一条交易信息
        String isCreateTran = (String) map.get("isCreateTran");
        String amountOfMoney = (String) map.get("amountOfMoney");
        String tradeName = (String) map.get("tradeName");
        String expectedClosingDate = (String) map.get("expectedClosingDate");
        String stage = (String) map.get("stage");
        String activityId = (String) map.get("activityId");
        if ("true".equals(isCreateTran)) {
            Tran tran = new Tran();
            tran.setId(UUIDUtils.getUUID());
            tran.setOwner(user.getId());
            tran.setMoney(amountOfMoney);
            tran.setName(tradeName);
            tran.setExpectedDate(expectedClosingDate);
            tran.setCustomerId(customer.getId());
            tran.setStage(stage);
            tran.setSource(clue.getSource());
            tran.setActivityId(activityId);
            tran.setContactsId(contacts.getId());
            tran.setCreateBy(clue.getCreateBy());
            tran.setCreateTime(clue.getCreateTime());
            tran.setEditBy(clue.getEditBy());
            tran.setEditTime(clue.getEditTime());
            tran.setContactSummary(clue.getContactSummary());
            tran.setNextContactTime(clue.getNextContactTime());
            tran.setDescription(clue.getDescription());
            tranMapper.insertSelective(tran);

            //7.如果创建了一条交易，则创建一条交易历史信息
            TranHistory tranHistory = new TranHistory();
            tranHistory.setId(UUIDUtils.getUUID());
            tranHistory.setStage(stage);
            tranHistory.setMoney(amountOfMoney);
            tranHistory.setExpectedDate(expectedClosingDate);
            tranHistory.setCreateTime(clue.getCreateTime());
            tranHistory.setCreateBy(clue.getCreateBy());
            tranHistory.setTranId(tran.getId());
            tranHistoryMapper.insertSelective(tranHistory);

        }

        //8.删除该转换的线索
        clueMapper.deleteByPrimaryKey(clueId);
        //9.删除该线索下的所有评论
        clueRemarkMapper.deleteByClueId(clueId);
        //10.删除该线索关联的市场活动
        clueActivityRelationMapper.deleteByClueId(clueId);

        return 1;
    }
}
